﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WucCompaign : System.Web.UI.UserControl
{
    Adminestrator admin = new Adminestrator();
    Compains comp = new Compains();
    protected void Page_Load(object sender, EventArgs e)
    {
        DataRow[] dr1 = BaseClass.RunSelect("select ID,LNAME from EMPLOYEES Where JOB='Doctor'").Select();
        DataRow[] dr2 = BaseClass.RunSelect("select ID,LNAME from EMPLOYEES Where JOB='Recieptionist'").Select();
        DataRow[] dr3 = BaseClass.RunSelect("select ID,LNAME from EMPLOYEES Where JOB='Analyser'").Select();
        DataRow[] dr4 = BaseClass.RunSelect("select ID,LNAME from EMPLOYEES Where JOB='Nurse'").Select();
        foreach (DataRow dR in dr1)
            DoctorDDl.Items.Add(new ListItem(dR[1].ToString(),dR[0].ToString()));
       foreach (DataRow dR in dr3)
           AnalyserDDl.Items.Add(new ListItem(dR[1].ToString(), dR[0].ToString()));
       foreach (DataRow dR in dr2)
           RecieptionistDDl.Items.Add(new ListItem(dR[1].ToString(), dR[0].ToString()));
       foreach (DataRow dR in dr4)
           NurseDDL.Items.Add(new ListItem(dR[1].ToString(), dR[0].ToString()));
     }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt=admin.SearchCompain(RBLSearch.SelectedValue, txtSearch.Text);
        if (dt.Rows.Count > 0)
        {
            grdv.DataSource = dt;
            grdv.DataBind();
            lblMsg.Text = "Select Any Employee To Update OR Remove .. ";
        }
        else
        {
            grdv.DataSource = dt;
            grdv.DataBind();
            lblMsg.Text = "There Is No Result For This Value Of Search!";
        }
        grdv.SelectedIndex = -1;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        MultiView2.ActiveViewIndex = 0;
        grdv.SelectedIndex = -1;
        btnRemove.Enabled = false;
        btnUpdate.Enabled = false;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        comp.Code = txtCode.Text;
        comp.EndDate= txtEdate.Text;
        comp.StartDate = txtBdate.Text;
        comp.Type = RBLType.SelectedValue;
        comp.Center = DDlCenter.SelectedValue;
        comp.Place = DDLPlace.SelectedValue;
        admin.Compains = comp;
        string[] EmpArray={DoctorDDl.SelectedValue,RecieptionistDDl.SelectedValue,AnalyserDDl.SelectedValue,NurseDDL.SelectedValue};
        if (txtCode.ReadOnly)
        {
            if (admin.UpdateCampaign())
            {
                lblMsg.Text = "Compaign Updated Successfully";
                MultiView2.ActiveViewIndex = 0;
            }
            else
                lblMsg.Text = "Compaign not Updated";
        }
        else
        {
            if (admin.CreateCompain()&&admin.AddComp_Emp(txtCode.Text,EmpArray))
            {
                lblMsg.Text = "Compaign Added Successfully";
                MultiView2.ActiveViewIndex = 0;
            }
            else
            {
                lblMsg.Text = "compaign not Added";
                MultiView2.ActiveViewIndex = 0;
            }
        }
        btnUpdate.Enabled = false;
        btnRemove.Enabled = false;
        grdv.SelectedIndex = -1;
    }
    
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (admin.CancelCompain(grdv.SelectedRow.Cells[1].Text))
            lblMsg.Text = "Compaing Deleted Successfully";
        else
            lblMsg.Text = "Compaign not Deleted";
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //MultiView1.ActiveViewIndex = 1;
        txtCode.Text = grdv.SelectedRow.Cells[1].Text;
        txtBdate.Text = grdv.SelectedRow.Cells[2].Text;
        txtEdate.Text = grdv.SelectedRow.Cells[3].Text;
        RBLType.SelectedValue = grdv.SelectedRow.Cells[4].Text;
        DDlCenter.SelectedValue = grdv.SelectedRow.Cells[5].Text;
        DDLPlace.SelectedValue = grdv.SelectedRow.Cells[6].Text;
   
        txtCode.ReadOnly = true;
        MultiView2.ActiveViewIndex = 1;
    }
    protected void grdv_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnRemove.Enabled = true;
        btnUpdate.Enabled = true;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        txtCode.ReadOnly = false;
        txtBdate.Text = "";
        txtCode.Text = "";
        txtEdate.Text = "";
        MultiView2.ActiveViewIndex = 1;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
 
    }
    protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
    {

    }
}